<template>
    <l-layout class="l-tab-page" v-if="!lr_loadPage">
        <l-panel>
            <template #toolLeft >
                <div class="l-panel--item" >
                    <el-input :placeholder="$t('请输入编码/名称')" v-model="keyWord"  @keyup.enter.native="hanleSearch" size="mini" >
                        <el-button slot="append" icon="el-icon-search" @click="hanleSearch" ></el-button>
                    </el-input>
                </div>
            </template>
            <template #toolRight >
                <l-tool-btns @click="handleAdd()" >
                    <l-excel-btns></l-excel-btns>
                </l-tool-btns>
            </template>
            <l-table 
                :loading="tableLoading"
                :columns="columns"
                :dataSource="tableData" 
                :isPage="true"
                :pageTotal="tableTotal"
                :tablePage.sync="tableCurrentPage"

                @loadPageData="turnTablePage"
                >
                <l-table-btns :btns="tableBtns" @click="lr_handleTableBtnClick" ></l-table-btns>
            </l-table>
        </l-panel>

        <l-drawer 
            :title="formTitle"
            :visible.sync="formVisible"
            @ok="handleSave"
            @close="handleCloseForm"
            @opened="handleOpenedForm"
            >
            <my-form ref="form" ></my-form>       
        </l-drawer>
    </l-layout>
</template>

<script>
const api = window.$api.data.map
import MyForm from './form'
export default {
    components: {
        MyForm
    },
    data () {
        return {
            lr_isPage:true,
            // 表格
            columns: [
                {label:'地图名称',prop:'f_Name'},
            ],
            tableBtns:[
                {prop:'Edit',label:this.$t('编辑'),width:40},
                {prop:'Delete',label:this.$t('删除'),width:40}
            ],
            tableLoading:false,
            tableTotal:0,
            tablePageSize:50,
            tableCurrentPage:1,
            tableData: [],
            keyWord:'',

            formVisible:false,
            formTitle:'',
            formEditRow:null,
            formEdit:false
        }
    },

    computed:{
    },
    mounted () {
        /*for(let i=0;i<100;i++){
            this.getCode('csbm')
        }*/
        this.loadTablePageData()
    },
    methods:{
        loadTablePageData(isNotFirst){
            if(!isNotFirst){
                this.tableCurrentPage = 1;
            }
            this.tableLoading = true;
            let queryData = {
                rows:this.tablePageSize,
                page:this.tableCurrentPage,
                // sidx:'f_Name DESC',
                keyword:this.keyWord
            }
            api.getList(queryData).then(res=>{
                const data = this.$deepClone(res.data.data)
                this.tableData = data.rows
                this.tableTotal = data.records
                this.tableLoading = false

            }).catch(()=>{
                this.tableData = []
                this.tableLoading = false
            })
        },
        turnTablePage({rows}){
            this.tablePageSize = rows
            this.loadTablePageData(true)
        },
        hanleSearch(){
            this.loadTablePageData()
        },

        // 表单部分
        handleAdd(){
            this.formEdit = false
            this.showForm('新增地图数据')
        },
        handleEdit($index,row){
            this.formEdit = true
            this.formEditRow = row
            this.showForm('编辑地图数据')
        },
        handleDelete($index,row){
            this.$confirm(
                this.$t('此操作将永久删除该数据, 是否继续?'), 
                this.$t('提示'), {
                confirmButtonText: this.$t('确定'),
                cancelButtonText: this.$t('取消'),
                type: 'warning'
            }).then(() => {
                api.delObj(row.f_Id).then(()=> {
                    this.$message({
                        type: 'success',
                        message: this.$t('删除成功!')
                    });
                    this.loadTablePageData()
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: this.$t('已取消删除')
                })          
            })
        },
        handleSave(showLoading,hideLoading){
            this.$refs.form.validateForm(()=>{
                showLoading('加载中...')
                const formData = this.$refs.form.getForm()
                if(this.formEdit){// 修改数据
                    api.updateObj(formData.f_Id,this.$deepClone(formData)).then(()=> {
                        hideLoading()
                        this.formVisible = false
                        this.$message({
                            type: 'success',
                            message: this.$t('更新成功!')
                        })
                        this.loadTablePageData()
                    }).catch(() => {
                        hideLoading()
                    })
                }
                else{// 新增数据
                    api.addObj(this.$deepClone(formData)).then(()=> {
                        hideLoading()
                        this.formVisible = false
                        this.$message({
                            type: 'success',
                            message: this.$t('添加成功!')
                        })
                        this.loadTablePageData()
                    }).catch(() => {
                        hideLoading()
                    })
                }
            })
        },
        handleOpenedForm(showLoading,hideLoading){
            if(this.formEdit){
                showLoading("加载数据中...");
                api.getObj(this.formEditRow.f_Id).then((res)=> {
                    res.data.data.f_Name = this.formEditRow.f_Name
                    this.$refs.form.setForm(res.data.data);
                    hideLoading();
                }).catch(()=>{
                    this.$message({
                        type: 'warning',
                        message: this.$t('获取编辑数据失败!')
                    });
                    hideLoading();
                    this.formVisible = false;
                });   
            }
        },
        handleCloseForm(){
            this.$refs.form.resetForm()
        },
        showForm(text){
            this.formTitle = text
            this.formVisible = true
        }
    }

}
</script>